// 蒸汽单车产量温度卡
var numberFormate = iot.getNumberFormat("0.00");
var timeFormatMap = {
    'year': "%Y",
    'month': "%Y-%m",
    'day': "%Y-%m-%d"
};
dev.action = function(resp,param){
    //获取工厂名称
    var factoryName = getFactoryName(param.workSpaceName);
    var theTime = param.time;
    var workSpace = param.workSpace;
    var jsData =  iot.list();
    var db = iot.db();
    var coldResult = db.qry("select esp.name,SUM(dev_win.dvalue) totalSum from iot_dev_data_win dev_win," +
        " (select * from energy_struc_prop where type = 1 and field_type = '用量' and energy_struc_id = "+ workSpace +") esp" +
        " where dev_win.dev_id = esp.dev_id" +
        " and dev_win.data_type = esp.name" +
        " and dev_win.the_time like '"+ theTime +"%'");

    var yieldId = getFactoryYieldId(factoryName)

    var yieldResult = getFactoryYield(yieldId, theTime)

    var totalUseNum = 0;
    var bicycleUseNum = 0;
    var yieldUseNum = 0;

    coldResult.forAll(function (item) {
        if(item.totalSum){
            totalUseNum = numberFormate.format(item.totalSum);
        }
    });
    if (yieldResult) {
        yieldUseNum = yieldResult.totalSum;
    }
    if(yieldUseNum && yieldUseNum !== 0){
        bicycleUseNum = numberFormate.format(totalUseNum/yieldUseNum);
    }

    //start KPI
    var sn = "H002";
    //获取目标id
    var targetValueIdResult = db.qry("select es2.id from energy_struc es1\n" +
        "JOIN energy_struc es2\n" +
        "on es1.id = es2.pid\n" +
        "where es1.id =" + workSpace);
    var targetValueId = '';
    targetValueIdResult.forAll(function(item){
        targetValueId = targetValueId + item.id + ","
    });
    targetValueId = targetValueId + 0;

    if(targetValueId == "0"){
        targetValueId = workSpace;
    }
    var targetValueTime = theTime;
    if(theTime && theTime.length() == 10){
        targetValueTime = theTime.substring(0,7);
    }
    //获取目标值
    var targetValueResult = db.qry("select esp.name,SUM(win.dvalue) totalSum \n" +
        "from iot_dev_data_win win\n" +
        "left join energy_struc_prop esp\n" +
        "on win.dev_id = esp.dev_id\n" +
        "where esp.sn = '"+ sn +"'\n" +
        "and esp.energy_struc_id in( " +targetValueId+ " )\n" +
        "and win.the_time like '"+ targetValueTime +"%'\n" +
        "and win.data_type = esp.name");
    var targetValue = 0;
    targetValueResult.forAll(function(item){
        targetValue = item.totalSum
    });

    //计算总KPI
    var totalKPI = 0;
    var totalKPIResult = '+0%';
    if(targetValue && targetValue != 0){
        if(targetValue >= totalUseNum){//如果目标值大于总用值
            totalKPI = totalUseNum / targetValue;
            totalKPIResult = "+"+iot.getPercentage(totalKPI);
        }else{
            totalKPI = (totalUseNum - targetValue)/targetValue;
            totalKPIResult = "-"+iot.getPercentage(totalKPI);
        }
    }
    //计算单车KPI
    var oneKPI = 0;
    var oneKPIResult = '+0%';
    //单车KPI
    var oneTargetValue = 0;
    if(yieldUseNum && yieldUseNum != 0){
        oneTargetValue = targetValue/yieldUseNum;
    }
    if(oneTargetValue != 0){
        if(oneTargetValue >= bicycleUseNum){//如果目标值大于总用值
            oneKPI = bicycleUseNum / oneTargetValue;
            oneKPIResult = "+"+iot.getPercentage(oneKPI);
        }else{
            oneKPI = (bicycleUseNum - oneTargetValue)/oneTargetValue;
            oneKPIResult = "-"+iot.getPercentage(oneKPI);
        }
    }
    //end KPI
    //start环比
    var timeType = iot.getTimeByFormat(theTime);
    var format = timeFormatMap[timeType];
    var theTimeOr = '';
    var timeTemp = theTime;
    if(theTime && theTime.length() == 4){
        timeTemp = theTime + "-01-01";
        theTimeOr = "DATE_FORMAT(date_add('"+timeTemp+"', interval -1 year),'%Y') time";
        print(timeTemp);
        print(theTimeOr);
    }
    if(theTime && theTime.length() == 7){
        timeTemp = theTime + "-01";
        theTimeOr = "DATE_FORMAT(date_add('"+timeTemp+"', interval -1 month),'%Y-%m') time";
    }
    if(theTime && theTime.length() == 10){
        theTimeOr = "DATE_FORMAT(date_add('"+timeTemp+"', interval -1 day),'%Y-%m-%d') time";
    }
    var theTimeOrResult = db.qry("select " + theTimeOr);
    theTimeOrResult.forAll(function(item,ix){
        theTimeOr = item.time;
    });
    var ratioValueResult = db.qry("select esp.name,SUM(win.dvalue) totalSum,DATE_FORMAT(win.the_time,'"+ format +"') time\n" +
        "from iot_dev_data_win win\n" +
        "LEFT JOIN energy_struc_prop esp\n" +
        "on win.dev_id = esp.dev_id\n" +
        "where esp.field_type = '用量'\n" +
        "and esp.energy_struc_id = "+workSpace+"\n" +
        "and (win.the_time like '"+ theTime +"%'\n" +
        "or win.the_time like '"+ theTimeOr +"%')\n" +
        "GROUP BY time\n" +
        "ORDER BY time desc\n" +
        "limit 2");
    var lastYieldResult = getFactoryYield(yieldId, theTimeOr)
    var nowTimeTotalSum = 0;
    var lastTimeTotalSum = 0;
    var lastYieldUseNum = 0;
    ratioValueResult.forAll(function(item,ix){
        if(ix==0){
            nowTimeTotalSum = item.totalSum;
        }
        if(ix==1){
            lastTimeTotalSum = item.totalSum;
        }
    });
    if (lastYieldResult) {
        lastYieldUseNum = lastYieldResult.totalSum;
    }

    //计算总环比
    var ratioResult = '+0%';
    var ratioTotalSum = nowTimeTotalSum - lastTimeTotalSum;
    if(lastTimeTotalSum && lastTimeTotalSum != 0){
        var ratioValue = ratioTotalSum / lastTimeTotalSum;
        if(ratioTotalSum >= 0){
            ratioResult = "+"+iot.getPercentage(ratioValue);
        }else{
            ratioResult = iot.getPercentage(ratioValue);
        }
    }
    //计算单车环比
    var oneNowTimeTotalSum = 0;
    var oneLastTimeTotalSum = 0;
    if(yieldUseNum && yieldUseNum != 0){
        oneNowTimeTotalSum = nowTimeTotalSum/yieldUseNum;
    }
    if(lastYieldUseNum && lastYieldUseNum != 0){
        oneLastTimeTotalSum = lastTimeTotalSum/lastYieldUseNum;
    }
    var oneRatioResult = '+0%';
    var oneRatioTotalSum = oneNowTimeTotalSum - oneLastTimeTotalSum;
    if(oneLastTimeTotalSum && oneLastTimeTotalSum != 0){
        var oneRatioValue = oneRatioTotalSum / oneLastTimeTotalSum;
        if(oneRatioTotalSum >= 0){
            oneRatioResult = "+"+iot.getPercentage(oneRatioValue);
        }else{
            oneRatioResult = iot.getPercentage(oneRatioValue);
        }
    }
    //end环比

    jsData.add({unit: "T", lastPeriod: ratioResult, type: "总蒸汽", useNum: totalUseNum, kpi: totalKPIResult, ratioValue: ratioTotalSum, kpiValue: targetValue});
    jsData.add({unit: "T/辆", lastPeriod: oneRatioResult, type: "单车蒸汽", useNum: bicycleUseNum, kpi: oneKPIResult, oneRatioValue: oneRatioTotalSum, kpiValue: oneTargetValue});
    jsData.add({unit: "辆", type: "总产量", useNum: yieldUseNum});
    jsData.add({unit: "℃", type: "平均温度", useNum: 27.5});

    resp.setJsData(jsData);
}